<?php

namespace app\admin\controller;

use think\Db;
use app\common\validate\BannerValidate;

class Banner extends Base
{
	protected $datas;

	/**
	 * banner列表
	 * @Author   Yichen
	 * @DateTime 2020-05-02T13:56:56+0800
	 */
	public function BannerList()
	{
		$data = Db::name('t_banner')->paginate(6);
		$this->assign('data', $data);
		return $this->fetch();
	}

	/**
	 * 添加
	 * @Author   Yichen
	 * @DateTime 2020-05-02T13:57:01+0800
	 */
	public function addBanner()
	{
		if (request()->isPost()) {
			$this->datas = $this->params;
			$addValidate = new BannerValidate();
			if (!$addValidate->scene('add')->check($this->datas)) {
	            $this->error($addValidate->getError());
	        }
	        $name = $this->datas['name'];
	        $image = $this->datas['image'];
	        $description = $this->datas['description'];
	        $url = $this->datas['url'];
	        $insertData = [
	        	'name' => $name,
	        	'image' => $image,
	        	'description' => $description,
	        	'url' => $url,
	        	'create_time' => time()
	        ];
	        $result = Db::name('t_banner')->insert($insertData);
	        if (!$result) {
	        	$this->error('添加失败');
	        }
	        $this->success('添加成功');
		}
		return $this->fetch();
	}

	/**
	 * 上传图片
	 * @Author   Yichen
	 * @DateTime 2020-05-02T13:57:20+0800
	 * @return   [type]                   [description]
	 */
	public function uploadImg()
	{
		$path = 'static'. DS . 'upload' . DS . 'banner';
		$size = 58400*100;
		$type = 'jpg,png,gif,jpeg';
		$uploadResult = uploadFile('file', $path, $size, $type);
		if (!$uploadResult[0]) {
			$this->error($uploadResult[1]);
		}
		$this->success('上传成功', '', $uploadResult[1]);
	}

	/**
	 * 修改
	 * @Author   Yichen
	 * @DateTime 2020-05-02T14:26:25+0800
	 * @return   [type]                   [description]
	 */
	public function editBanner()
	{
		$this->datas = $this->params;
		if (request()->isPost()) {
			$editValidate = new BannerValidate();
			if (!$editValidate->scene('edit')->check($this->datas)) {
	            $this->error($editValidate->getError());
	        }
	        $id = $this->datas['id'];
	        $updateData['name'] = $this->datas['name'];
	        $updateData['image'] = $this->datas['image'];
	        $updateData['description'] = $this->datas['description'];
	        $updateData['url'] = $this->datas['url'];
	        $updateData['update_time'] = time();
	        $result = Db::name('t_banner')->where('id', $id)->update($updateData);
	        if (!$result) {
	        	$this->error('修改失败');
	        }
	        $this->success('修改成功');
		}
		$id = $this->datas['id'];
		$data = Db::name('t_banner')->where('id', $id)->find();
		$this->assign('data', $data);
		return $this->fetch();
	}

	/**
	 * 禁用或启用
	 * @Author   Yichen
	 * @DateTime 2020-05-01T16:16:12+0800
	 */
	public function ProhibitOrEnable()
	{
		if (request()->isGet()) {
			$this->datas = $this->params;
			$ProOrEnaValidate = new BannerValidate();
			if (!$ProOrEnaValidate->scene('ProOrEna')->check($this->datas)) {
				$this->error($ProOrEnaValidate->getError());
			}
			$id = $this->datas['id'];
			$msg = $this->datas['msg'];
			if ($msg == '禁用') {
				$status = 0;
			} else {
				$status = 1;
			}
			$result = Db::name('t_banner')->where('id', $id)->update(['status' => $status]);
			if (!$result) {
				$this->error('删除失败');
			}
			$this->success('删除成功');
		}
		$this->error('非法请求');
	}


	public function delBanner()
	{

	}
}
